package com.qbw.app.module.vehicle.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.View;

import com.http.okhttp.HttpTask;
import com.lib.util.ScreenUtil;
import com.qbw.actionmanager.ActionManager;
import com.qbw.app.Constant;
import com.qbw.app.R;
import com.qbw.app.XApplication;
import com.qbw.app.action.ElectricAreaAddAction;
import com.qbw.app.action.ElectricAreaDeleteAction;
import com.qbw.app.action.ElectricAreaUpdateAction;
import com.qbw.app.databinding.VehicleActivityElectricareaBinding;
import com.qbw.app.model.ElectricArea;
import com.qbw.app.module.common.activity.CommonActivity;
import com.qbw.app.module.vehicle.adapter.VehicleAdapter;
import com.qbw.app.request.impl.VehicleRequest;
import com.qbw.core.holder.EmptyHolder;
import com.qbw.core.model.Empty;
import com.qbw.customview.RefreshLoadMoreLayout;

/**
 * @author QBW
 * @date 2019/9/26
 */
public class VehicleElectricAreaActivity extends CommonActivity<VehicleActivityElectricareaBinding> {

    private VehicleAdapter mAdapter;
    private int mPage = 1;

    public static void start(Context context) {
        Intent intent = new Intent(context, VehicleElectricAreaActivity.class);
        if (!(context instanceof Activity)) {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        context.startActivity(intent);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.vehicle_activity_electricarea;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding.title.setListener(this);
        mBinding.rcv.setLayoutManager(new LinearLayoutManager(this));
        mBinding.rcv.addItemDecoration(new InnerDecoration());
        mAdapter = new VehicleAdapter(this);
        mBinding.rcv.setAdapter(mAdapter);
        mBinding.rll.init(new RefreshLoadMoreLayout.Config(this));
        mBinding.rll.startAutoRefresh();
        mActionManager.addInterestedAction(this, ElectricAreaAddAction.class);
        mActionManager.addInterestedAction(this, ElectricAreaDeleteAction.class);
        mActionManager.addInterestedAction(this, ElectricAreaUpdateAction.class);
    }

    @Override
    public void onRightAreaClick() {
        super.onRightAreaClick();
        VehicleAddElectricAreaActivity.start(this);
    }

    @Override
    public void onHttpSuccess(HttpTask httpTask, Object entity) {
        super.onHttpSuccess(httpTask, entity);
        String method = httpTask.getMethod();
        if (method.equals(VehicleRequest.METHOD_ELECTRIC_AREA_LIST)) {
            ElectricArea.ResponseList responseList = (ElectricArea.ResponseList) entity;
            boolean refresh = mPage == 1;
            if (refresh) {
                mAdapter.clear();
            }
            mAdapter.addChild(responseList.getData().getList());
            int size = responseList.getData() != null ? responseList.getData().size() : 0;
            if (refresh && size <= 0) {
                mAdapter.showEmpty(new Empty(0, EmptyHolder.ModelId.M_VEHICLE_NO_ELECTRIC_AREA));
            }
            boolean noMore = size < Constant.PAGESIZE_20;
            if (!noMore) {
                mPage++;
            }
            if (refresh) {
                mBinding.rll.stopRefreshNoMoreData(noMore);
            } else {
                mBinding.rll.stopLoadMoreNoMoreData(noMore);
            }
        }
    }

    @Override
    public void onHttpFailed(HttpTask httpTask, int errorCode, String message) {
        super.onHttpFailed(httpTask, errorCode, message);
        String method = httpTask.getMethod();
        if (method.equals(VehicleRequest.METHOD_ELECTRIC_AREA_LIST)) {
            boolean refresh = mPage == 1;
            if (refresh) {
                mBinding.rll.stopRefresh();
            } else {
                mBinding.rll.stopLoadMore();
            }
        }
    }

    @Override
    public void onActionTriggered(Object action) {
        super.onActionTriggered(action);
        if (action instanceof ElectricAreaAddAction || action instanceof ElectricAreaUpdateAction) {
            onRefresh();
        } else if (action instanceof ElectricAreaDeleteAction) {
            mAdapter.deleteElectricArea(((ElectricAreaDeleteAction) action).getAction());
        }
    }

    @Override
    public void onRefresh() {
        super.onRefresh();
        mPage = 1;
        mGeneralManager.getRequestManager()
                       .getVehicleRequest()
                       .getElectricAreaList(mPage, Constant.PAGESIZE_20, this)
                       .execute();
    }

    @Override
    public void onLoadMore() {
        super.onLoadMore();
        mGeneralManager.getRequestManager()
                       .getVehicleRequest()
                       .getElectricAreaList(mPage, Constant.PAGESIZE_20, this)
                       .execute();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ActionManager.getInstance().removeInterestedActions(this);
    }

    private static class InnerDecoration extends RecyclerView.ItemDecoration {

        private Context mContext = XApplication.getInstance();
        private int mSpace1;
        private Paint mPaint;

        public InnerDecoration() {
            mSpace1 = ScreenUtil.dp2px(mContext, 15);
            mPaint = new Paint();
            mPaint.setAntiAlias(true);
            mPaint.setColor(Color.parseColor("#2B333C"));
        }

        @Override
        public void getItemOffsets(@NonNull Rect outRect,
                                   @NonNull View view,
                                   @NonNull RecyclerView parent,
                                   @NonNull RecyclerView.State state) {
            super.getItemOffsets(outRect, view, parent, state);
            int ap = parent.getChildAdapterPosition(view);
            if (ap == RecyclerView.NO_POSITION) {
                return;
            }
            if (ap == 0) {
                outRect.top = mSpace1;
            }
        }

        @Override
        public void onDrawOver(@NonNull Canvas c,
                               @NonNull RecyclerView parent,
                               @NonNull RecyclerView.State state) {
            super.onDrawOver(c, parent, state);
            int cc = parent.getChildCount();
            View view;
            for (int i = 0; i < cc; i++) {
                view = parent.getChildAt(i);
                c.drawRect(view.getLeft() + mSpace1,
                           view.getTop(),
                           view.getRight() - mSpace1,
                           view.getTop() + 1,
                           mPaint);
            }
            if (cc > 0) {
                view = parent.getChildAt(cc - 1);
                c.drawRect(view.getLeft() + mSpace1,
                           view.getBottom() - 1,
                           view.getRight() - mSpace1,
                           view.getBottom(),
                           mPaint);
            }
        }
    }
}
